package com.javaee.xiangyang.service.impl;

import com.javaee.xiangyang.common.ServiceResultEnum;
import com.javaee.xiangyang.dto.ShopCarGoods;
import com.javaee.xiangyang.entity.Goods;
import com.javaee.xiangyang.entity.ShopCar;
import com.javaee.xiangyang.entity.User;
import com.javaee.xiangyang.mapper.GoodsMapper;
import com.javaee.xiangyang.mapper.ShopCarMapper;
import com.javaee.xiangyang.mapper.UserMapper;
import com.javaee.xiangyang.service.ShopCarService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ShopCarServiceImpl implements ShopCarService {

    @Resource
    UserMapper userMapper;

    @Resource
    ShopCarMapper shopCarMapper;

    @Resource
    GoodsMapper goodsMapper;

    /**
     * 用户存放商品到购物车
     */
    @Override
    @Transactional
    public ServiceResultEnum insertShopCar(ShopCar shopCar) {
        if (userMapper.findByUserAccount(new User(shopCar.getShopUserAccount())) != null) {
            //用户存在
            if (goodsMapper.findGoodsByGoodsId(new Goods(shopCar.getShopGoodsId())) != null) {
                //商品存在
                if (shopCarMapper.findCarByAccountAndId(shopCar) == null) {
                    //用户车已经没有此条信息
                    shopCarMapper.insertShopCar(shopCar);
                    return ServiceResultEnum.SUCCESS;
                }
                return ServiceResultEnum.SHOPPING_CART_IS_EXIST;
            }
            return ServiceResultEnum.GOODS_NOT_EXIST;
        }
        return ServiceResultEnum.LOGIN_NAME_NULL;
    }

    /**
     * 根据购物车ID查找具体信息
     */
    @Override
    public ShopCarGoods findCarById(ShopCar shopCar) {
        ShopCarGoods shopCarGoods = new ShopCarGoods();
        ShopCar carById = shopCarMapper.findCarById(shopCar);
        if (carById != null) {
            //对应购物车存在
            Goods goodsByGoodsId = goodsMapper.findGoodsByGoodsId(new Goods(carById.getShopGoodsId()));
            shopCarGoods.setGoods(goodsByGoodsId);
            shopCarGoods.setShopCar(carById);
        }
        return shopCarGoods;
    }

    /**
     * 根据用户查找购物车内商品
     */
    @Override
    public List<ShopCarGoods> findCarByUserAccount(ShopCar shopCar) {
        List<ShopCarGoods> shopCarGoodsList = new ArrayList<>();
        if (userMapper.findByUserAccount(new User(shopCar.getShopUserAccount())) != null) {
            //用户存在
            List<ShopCar> carByUserAccount = shopCarMapper.findCarByUserAccount(shopCar);
            if (carByUserAccount != null) {
                //用户购物车不为空
                for (ShopCar shopCars : carByUserAccount) {
                    ShopCarGoods shopCarGoods = new ShopCarGoods();
                    shopCarGoods.setShopCar(shopCars);
                    shopCarGoods.setGoods(goodsMapper.findGoodsByGoodsId(new Goods(shopCars.getShopGoodsId())));
                    shopCarGoodsList.add(shopCarGoods);
                }
            }
        }
        return shopCarGoodsList;
    }

    /**
     * 更新购物车中某一商品的数量
     */
    @Override
    @Transactional
    public ServiceResultEnum updateCarNumberBy(ShopCar shopCar) {
        if (shopCarMapper.findCarById(shopCar) != null) {
            //此购物车存在
            shopCarMapper.updateCarNumberBy(shopCar);
            return ServiceResultEnum.SUCCESS;
        }
        return ServiceResultEnum.SHOPPING_ITEM_ERROR;
    }

    /**
     * 用户删除购物车一条信息
     */
    @Override
    @Transactional
    public ServiceResultEnum deleteCarByUserAccountAndGoodsId(ShopCar shopCar) {
        if (shopCarMapper.findCarById(shopCar) != null &&
                shopCarMapper.findCarById(shopCar).getShopUserAccount().equals(shopCar.getShopUserAccount())) {
            //此购物车存在
            shopCarMapper.deleteCarByUserAccountAndGoodsId(shopCar);
            return ServiceResultEnum.SUCCESS;
        }
        return ServiceResultEnum.SHOPPING_CART_NOT_EXIST;
    }

    /**
     * 用户清空购物车
     */
    @Override
    @Transactional
    public ServiceResultEnum deleteCarByUserAccount(ShopCar shopCar) {
        shopCarMapper.deleteCarByUserAccount(shopCar);
        return ServiceResultEnum.SUCCESS;
    }
}
